<template>
  <div class="home">
    <el-container class="home_container">
      <el-header>
        <div class="home_header_box">
          <img src="../assets/img/CMall_Logo.png" />
          <span>后台管理系统</span>
        </div>
        <el-button type="danger" @click="logout" size="mini">
          注销
        </el-button>
      </el-header>
      <el-container>
        <el-aside :width="isCollapse ? '64px' : '180px'">
          <div class="toggle_button" @click="toggleCollapse">
            <i :class="isCollapse ? 'el-icon-s-unfold' : 'el-icon-s-fold'"></i>
          </div>
          <el-menu
            background-color="#30475e"
            text-color="#fff"
            active-text-color="#F05454"
            unique-opened
            router
            :collapse="isCollapse"
            :collapse-transition="false"
            :default-active="activePath"
          >
            <!-- 一级菜单 -->
            <el-submenu
              :index="item.id + ''"
              v-for="item in menuList"
              :key="item.id"
            >
              <template slot="title">
                <i :class="iconsObj[item.id]"></i>
                <span>{{ item.authName }}</span>
              </template>

              <!-- 二级菜单 -->
              <el-menu-item
                :index="'/' + subitem.path"
                v-for="subitem in item.children"
                :key="subitem.id"
                @click="saveNavState('/' + subitem.path)"
              >
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{ subitem.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      iconsObj: {
        '125': 'iconfont icon-users',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      isCollapse: false,
      activePath: ''
    }
  },
  created() {
    this.getMenuList()
    this.activePath = sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      sessionStorage.clear()
      location.reload()
    },

    // 获取侧边导航栏列表
    getMenuList() {
      this.$http.get('menus').then(res => {
        if (res.data.meta.status !== 200) {
          return this.$message.error(res.data.meta.msg)
        } else {
          this.menuList = res.data.data
        }
      })
    },

    // 侧边导航栏折叠
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },

    // 保存当前选择导航栏选项
    saveNavState(activePath) {
      sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.home {
  height: 100%;
  width: 100%;
}
.home_container {
  height: 100%;
  width: 100%;
}
.el-header {
  background: #222831;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-left: 0;
  .home_header_box {
    display: flex;
    align-items: center;
    height: 100%;
    img {
      height: 80%;
      line-height: 100%;
    }
    span {
      color: white;
      font-size: 20px;
      font-weight: 600;
      margin-left: 20px;
    }
  }
}
.el-aside {
  background: #30475e;
  .toggle_button {
    height: 24px;
    color: white;
    line-height: 24px;
    text-align: center;
    cursor: pointer;
    background-color: #889eaf;
  }
  .el-menu {
    border-right: 0;
    .iconfont {
      margin-right: 10px;
    }
  }
}
.el-main {
  background: #dddddd;
}
</style>
